package com.liit.controller.weixin.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.liit.entity.CodeFormMap;
import com.liit.entity.OrgInfoFormMap;
import com.liit.entity.UserFormMap;
import com.liit.entity.WeixinUserFormMap;
import com.liit.mapper.WeixinUserMapper;
import com.liit.util.Common;
import com.liit.util.DictUtils;
import com.liit.util.RegionUtils;

/**
 * @author dingweibo
 * @Title: WeixinUserController.java
 * @Description: 个人中心
 * @date 2016年4月1日
 */
@Controller
@RequestMapping("/weixin/")
public class WeixinMenuController extends WeixinBaseController {

	@Resource
	WeixinUserMapper mapper;

	/********* 用户菜单 *********/

	@RequestMapping("user/myinfo")
	public String user_myinfo(Model model) throws Exception {
		WeixinUserFormMap weixinUser = getWeixinUserInfo();
		weixinUser.put("provinceName", RegionUtils.getRegionNameById(weixinUser.getStr("province")));
		weixinUser.put("cityName", RegionUtils.getRegionNameById(weixinUser.getStr("city")));
		weixinUser.put("areaName", RegionUtils.getRegionNameById(weixinUser.getStr("area")));
		model.addAttribute("weixinUser", weixinUser);
		model.addAttribute("politicalList", DictUtils.getDictByType("POLITICAL_TYPE"));
		return Common.BACKGROUND_PATH + "/weixin/user/myinfo";
	}

	@RequestMapping("user/integral")
	public String user_integral(Model model) throws Exception {
		model.addAttribute("weixinUser", getWeixinUserInfo());
		return Common.BACKGROUND_PATH + "/weixin/user/integral";
	}

	@RequestMapping("user/myparty")
	public String user_myparty(Model model) throws Exception {
		useJSSDK("/weixin/user/myparty.shtml");
		return Common.BACKGROUND_PATH + "/weixin/user/myparty";
	}

	@RequestMapping("user/nav")
	public String user_bbs(Model model) throws Exception {
		return Common.BACKGROUND_PATH + "/weixin/user/nav";
	}
	
	@RequestMapping("user/listparty")
	public String user_listparty(Model model) throws Exception {
		model.addAttribute("partyType", DictUtils.getDictByType("PARTY_TYPE"));
		// 使用jssdk
		HttpServletRequest request = getHttpServletRequest();
		useJSSDK(request.getRequestURL().toString() + (request.getQueryString() != null ? "?" + request.getQueryString() : ""));
		return Common.BACKGROUND_PATH + "/weixin/user/listParty";
	}

	/********* 机构菜单 *********/

	@RequestMapping("org/myinfo")
	public String org_myinfo(Model model) throws Exception {
		OrgInfoFormMap orgInfo = getOrgInfo();
		if (orgInfo == null) {
			return org_reg(model);
		}
		orgInfo.put("province", RegionUtils.getRegionNameById(orgInfo.getStr("province")));
		orgInfo.put("city", RegionUtils.getRegionNameById(orgInfo.getStr("city")));
		orgInfo.put("area", RegionUtils.getRegionNameById(orgInfo.getStr("area")));
		orgInfo.put("street", RegionUtils.getRegionNameById(orgInfo.getStr("street")));
		model.addAttribute("orgInfo", orgInfo);
		model.addAttribute("codeList", mapper.findByAttribute("orgInfoId", orgInfo.get("id").toString(), CodeFormMap.class));
		model.addAttribute("orgTypeList", DictUtils.getDictByType("ORG_TYPE"));
		List<UserFormMap> uList = mapper.findByAttribute("openid", orgInfo.getStr("openid"), UserFormMap.class);
		if (!uList.isEmpty()) {
			model.addAttribute("accountName", uList.get(0).getStr("accountName"));
		}
		return Common.BACKGROUND_PATH + "/weixin/org/myinfo";
	}

	@RequestMapping("org/integral")
	public String org_integral(Model model) throws Exception {
		OrgInfoFormMap orgInfo = getOrgInfo();
		if (orgInfo == null) {
			return org_reg(model);
		} else if (orgInfo.getInt("status") == Common.ORG_STATUS_APPLY || orgInfo.getInt("status") == Common.ORG_STATUS_REJECT) {
			return org_myinfo(model);
		}
		model.addAttribute("integral", orgInfo.getInt("integral"));
		return Common.BACKGROUND_PATH + "/weixin/org/integral";
	}

	@RequestMapping("org/myparty")
	public String org_myparty(Model model) throws Exception {
		OrgInfoFormMap orgInfo = getOrgInfo();
		if (orgInfo == null) {
			return org_reg(model);
		} else if (orgInfo.getInt("status") == Common.ORG_STATUS_APPLY || orgInfo.getInt("status") == Common.ORG_STATUS_REJECT) {
			return org_myinfo(model);
		}
		useJSSDK("/weixin/org/myparty.shtml");
		return Common.BACKGROUND_PATH + "/weixin/org/myparty";
	}

	@RequestMapping("org/reg")
	public String org_reg(Model model) throws Exception {
		OrgInfoFormMap orgInfo = getOrgInfo();
		if (orgInfo != null && (orgInfo.getInt("status") == Common.ORG_STATUS_APPLY || orgInfo.getInt("status") == Common.ORG_STATUS_PASS)) {
			return org_myinfo(model);
		}
		model.addAttribute("orgTypeList", DictUtils.getDictByType("ORG_TYPE"));
		return Common.BACKGROUND_PATH + "/weixin/org/reg";
	}

	@RequestMapping("org/apply")
	public String org_apply(Model model) throws Exception {
		OrgInfoFormMap orgInfo = getOrgInfo();
		if (orgInfo == null) {
			return org_reg(model);
		} else if (orgInfo.getInt("status") == Common.ORG_STATUS_APPLY || orgInfo.getInt("status") == Common.ORG_STATUS_REJECT) {
			return org_myinfo(model);
		}
		model.addAttribute("orgName", orgInfo.getStr("orgName"));
		model.addAttribute("typeList", DictUtils.getDictByType("PARTY_TYPE"));
		return Common.BACKGROUND_PATH + "/weixin/org/apply";
	}

	@RequestMapping("org/party/detail")
	public String org_party_detail(Model model) {
		return Common.BACKGROUND_PATH + "/weixin/org/party_detail";
	}
	
	/**
	 * 微信消息提示页面
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping("weixinMsg")
	public String weixinMsg(Model model) {
		return Common.BACKGROUND_PATH + "/weixin/user/weixinMsg";
	}
}
